<template>
	<view class="content b-t">
		<view class="mask-content">
			<!-- 优惠券页面，仿mt -->
			<view class="coupon-item" v-for="(item,index) in couponList" :key="index" @tap="getCoupon(item)">
				<view class="con">
					<view class="left">
						<text class="title">{{item.title}}</text>
						<text class="time" v-if="parseInt(item.term_of_validity_type, 10) === 0">{{ item.start_time | time }} ~ {{ item.end_time | time }}</text>
						<text class="time" v-else>自领取之日 {{ item.fixed_term }}天内有效</text>
					</view>
					<view class="right">
						<text class="price" v-if="item.money">{{item.money }}</text>
						<text class="price-discount" v-else>{{ `${item.discount}折` }}</text>
						<text>满{{ item.at_least }}可用</text>
					</view>
					<view class="circle l"></view>
					<view class="circle r"></view>
				</view>
				<view class="tips">
					<text v-show="item.range_type">
						{{ parseInt(item.range_type, 10) === 2 ? '部分游戏使用' : '全场游戏使用' }}
					</text>
					<text v-show="type">
						<text style="margin-right: 15upx;" v-show="parseInt(item.range_type || item.couponType.range_type, 10) === 0">可使用游戏</text>
						<text class="btn" v-show="parseInt(item.range_type || item.couponType.range_type, 10) === 2" @tap="show(item)">查看游戏</text>
						<text class="btn" @tap="navTo('/pages/category/category', 'tab')">去使用</text>
					</text>
					<text v-if="item.myGet" v-show="!type">
						{{parseInt(item.max_fetch, 10) === 0 ? '不限' : `每人限领${item.max_fetch}` }}
					</text>
					<text v-else v-show="!type">
						{{parseInt(item.max_fetch, 10) === 0 ? '不限' : `限领${item.max_fetch}` }}
					</text>
				</view>
			</view>
			<rf-load-more :status="loadingType"></rf-load-more>
			<empty :info="'暂无优惠券'" v-if="couponList.length === 0"></empty>
		</view>
	</view>
</template>

<script>
	/**
	 * @des 领取中心
	 *
	 * @author stav stavyan@qq.com
	 * @date 2020-01-13 11:28
	 * @copyright 2019
	 */
	import {couponList, couponReceive, myCouponList} from "../../api/userInfo";
	import rfLoadMore from '@/components/rf-load-more/rf-load-more.vue';
	import empty from "@/components/empty";
	import moment from '@/utils/moment';
	export default {
		components: {
			rfLoadMore,
			empty,
		},
		data() {
			return {
				couponList: [],
				type: null,
				loadingType: 'more',
				token: null,
				page: 1
			}
		},
		filters: {
			time(val) {
				return moment(val * 1000).format('YYYY-MM-DD HH:mm')
			}
		},
		onLoad(options){
			this.type = options.type;
			this.initData();
		},
		//下拉刷新
		onPullDownRefresh(){
			this.page = 1;
			this.couponList = [];
			this.getCouponList('refresh');
		},
		//加载更多
		onReachBottom(){
			this.page ++;
			this.getCouponList();
		},
		methods: {
			/**
			 *@des 初始化数据
			 *@author stav stavyan@qq.com
			 *@blog https://stavtop.club
			 *@date 2019/11/18 09:57:30
			 */
			initData () {
				this.getCouponList();
			},
			// 统一跳转接口
			navTo(url, type){
				if(!this.token){
					url = '/pages/public/login';
				}
				if (type) {
					uni.switchTab({url});
				}
				if (url === 'login') {
					return
				} else {
					uni.navigateTo({
						url
					})
				}
			},
			/**
			 *@des 获取收货地址列表
			 *@author stav stavyan@qq.com
			 *@blog https://stavtop.club
			 *@date 2019/11/18 09:58:15
			 */
			async getCouponList (type) {
				uni.showLoading({title:'加载中...'});
				await this.$get(`${couponList}`, {
					page: this.page
				}).then(r=>{
					if (type === 'refresh') {
						uni.stopPullDownRefresh();
					}
					this.loadingType  = r.data.length === 10 ? 'more' : 'nomore';
					this.couponList = [ ...this.couponList, ...r.data];
				}).catch(err => {
					console.log(err);
				})
			},
			/**
			 *@des 获取优惠券
			 *@author stav stavyan@qq.com
			 *@blog https://stavtop.club
			 *@date 2019/11/25 13:41:19
			 */
			async getCoupon(item) {
				if (this.type) return;
				if (parseInt(item.is_get, 10) === 0) {
					this.$api.msg('该优惠券不可领取');
					return;
				}
				uni.showLoading({title: '领取中...'});
				await this.$post(`${couponReceive}`, {
					id: item.id
				}).then(r => {
					this.page = 1;
					this.couponList = [];
					this.getCouponList();
					this.$api.msg('领取成功');
				}).catch(err => {
					console.log(err)
				})
			}
		}
	}
</script>
<style lang='scss'>
	page{
		padding-bottom: 120upx;
		background: #f3f3f3;
	}
	.close {
		.btn {
			width: 240upx;
			margin: 20upx auto;
		}
	}
	.mask-content{
		width: 100%;
		transition: .3s;
		overflow-y:scroll;
		.coupon-item{
			display: flex;
			flex-direction: column;
			margin: 20upx 24upx;
			border-radius: 12upx;
			background: #fff;
			.con{
				display: flex;
				align-items: center;
				position: relative;
				height: 120upx;
				padding: 0 30upx;
				&:after{
					position: absolute;
					left: 0;
					bottom: 0;
					content: '';
					width: 100%;
					height: 0;
					border-bottom: 1px dashed #f3f3f3;
					transform: scaleY(50%);
				}
			}
			.left{
				display: flex;
				flex-direction: column;
				justify-content: center;
				flex: 1;
				overflow: hidden;
				height: 100upx;
			}
			.title{
				font-size: 32upx;
				color: $font-color-dark;
				margin-bottom: 10upx;
			}
			.time{
				font-size: 24upx;
				color: $font-color-light;
			}
			.right{
				display: flex;
				flex-direction: column;
				justify-content: center;
				align-items: center;
				font-size: 26upx;
				color: $font-color-base;
				height: 100upx;
			}
			.price {
				font-size: 44upx;
				color: $base-color;
				&:before{
					content: '￥';
					font-size: 34upx;
				}
			}
			.price-discount {
				font-size: 44upx;
				color: $base-color;
			}
			.tips{
				font-size: 24upx;
				color: $font-color-light;
				line-height: 60upx;
				padding-left: 30upx;
				overflow: hidden;
				display:inline-flex;
				justify-content: space-between;
				margin-right: 30upx;
				.btn {
					margin-left: 20upx;
					color: $font-color-base;
					font-size: $font-base;
				}
			}
			.circle{
				position: absolute;
				left: -6upx;
				bottom: -10upx;
				z-index: 10;
				width: 20upx;
				height: 20upx;
				background: #f3f3f3;
				border-radius: 100px;
				&.r{
					left: auto;
					right: -6upx;
				}
			}
		}
	}
	.coupon-none {
		text-align: center;
		font-size: $font-color-base;
		margin: 40upx 0;
	}
	.coupon-bottom-nav {
		background: #fff;
		height: 100upx;
		line-height: 60upx;
		position: fixed;
		overflow: hidden;
		bottom: 0;
		padding: 30upx 0 10upx;
		display: flex;
		width: 100%;
		.coupon-bottom-btn {
			text-align: center;
			color: $font-color-dark;
			font-size: $font-lg - 2upx;
			flex: 1;
			.right {
				font-size: $font-color-light;
				margin-left: 8upx;
			}
		}
		.coupon-bottom-btn:first-child {
			border-right: 1px solid rgba(0, 0, 0, 0.12); // 边框
		}
	}
</style>
